$OpenBSD: patch-time_c,v 1.4 2013/12/06 19:55:39 espie Exp $
--- time.c.orig	Thu Jan 31 04:33:06 2008
+++ time.c	Fri Dec  6 20:54:39 2013
@@ -20,6 +20,11 @@
 
 #include "scm.h"
 
+#ifdef __unix__
+# ifndef unix
+#  define unix 1
+# endif
+#endif
 #ifdef STDC_HEADERS
 # include <time.h>
 # ifdef M_SYSV
@@ -104,7 +109,6 @@
 #ifdef __OpenBSD__
 # include <sys/types.h>
 # include <sys/time.h>
-# include <sys/timeb.h>
 # define USE_GETTIMEOFDAY
 #endif
 #ifdef __TURBOC__
@@ -169,6 +173,7 @@
 # define USE_GETTIMEOFDAY
 #endif
 
+#ifndef USE_GETTIMEOFDAY
 #ifndef LACK_FTIME
 # ifdef HAVE_UNIX
 #  ifndef GO32
@@ -176,6 +181,7 @@
 #  endif
 # endif
 #endif
+#endif
 
 #ifdef __EMX__
 # define LACK_TIMES
@@ -308,18 +314,40 @@ SCM your_time()
 #else /* LACK_FTIME */
 # ifdef USE_GETTIMEOFDAY
 int scm_ftime(time_buffer)
-     struct timeb *time_buffer;
+     struct timeval *time_buffer;
 {
-  struct timezone t_z; struct timeval t_v;
-  if (gettimeofday(&t_v, &t_z) < 0) return -1;
-  time_buffer->timezone = t_z.tz_minuteswest;
-  time_buffer->dstflag = t_z.tz_dsttime;
-  time_buffer->millitm = t_v.tv_usec / 1000;
-  time_buffer->time = t_v.tv_sec;
+  struct timezone t_z; 
+  if (gettimeofday(time_buffer, &t_z) < 0) return -1;
   return 0;}
+
+struct timeval your_base = {0, 0};
+# define TIMETRIES 10
+SCM your_time()
+{
+  long tmp;
+  struct timeval time_buffer1;
+  struct timeval time_buffer2;
+  int cnt = 0;
+ tryagain:
+  cnt++;
+  scm_ftime(&time_buffer1);
+  scm_ftime(&time_buffer2);
+  if (time_buffer1.tv_sec==time_buffer2.tv_sec) {
+    if (time_buffer1.tv_usec > time_buffer2.tv_usec)
+      time_buffer2.tv_sec = time_buffer2.tv_sec + 1;
+  }
+  else if ((1 + time_buffer1.tv_sec)==time_buffer2.tv_sec) ;
+  else if (cnt < TIMETRIES) goto tryagain;
+  else { /* could not read two ftime()s within one second in 10 tries */
+    scm_warn("ftime()s too fast", "", MAKINUM(TIMETRIES));
+    return MAKINUM(-1);
+  }
+  tmp = CLKTCK*(time_buffer2.tv_usec - your_base.tv_usec);
+  tmp = CLKTCK*(time_buffer2.tv_sec - your_base.tv_sec) + tmp/1000000;
+  return MAKINUM(tmp);
+}
 # else /* USE_GETTIMEOFDAY */
 #  define scm_ftime ftime
-# endif /* USE_GETTIMEOFDAY */
 struct timeb your_base = {0};
 # define TIMETRIES 10
 SCM your_time()
@@ -346,6 +374,7 @@ SCM your_time()
   tmp = CLKTCK*(time_buffer2.time - your_base.time) + tmp/1000;
   return MAKINUM(tmp);
 }
+# endif /* USE_GETTIMEOFDAY */
 #endif /* LACK_FTIME */
 
 long my_base = 0;
